export default function ({ api, time, mixinParam }) {
  let copyApi = JSON.parse(JSON.stringify(api))
  //先混入mixinParam，再混入time
  //如果传了mixinParam
  if (mixinParam) {
    //如果是get
    if (copyApi.reqType === 'GET') {
      let urlParamObj = {}
      copyApi.urlParamList.forEach((item) => {
        if (item.type === 'variable_str') {
          urlParamObj[item.key] = item.value
        }
      })
      for (let mixinKey in mixinParam) {
        if (mixinParam.hasOwnProperty(mixinKey)) {
          if (urlParamObj[mixinKey]) {
            urlParamObj[mixinKey] = mixinParam[mixinKey]
          }
        }
      }
      copyApi.urlParamList = copyApi.urlParamList.filter(
        (item) => item.type !== 'variable_str'
      )
      for (let key in urlParamObj) {
        if (urlParamObj.hasOwnProperty(key)) {
          copyApi.urlParamList.push({
            key: key,
            value: urlParamObj[key],
            timeFormat: '',
            type: 'variable_str',
          })
        }
      }
      //如果是post
    } else {
      let type
      copyApi.body = copyApi.reqBody
      for (let head of copyApi.headers) {
        if (head.key === 'Content-Type') {
          type = head.value
        }
      }
      let bodyObj = {}
      //type 为'application/x-www-form-urlencoded'
      if (type === 'application/x-www-form-urlencoded') {
        if (!copyApi.body) copyApi.body = ''
        copyApi.body.split('&').forEach((item) => {
          bodyObj[item.split('=')[0]] = item.split('=')[1]
        })
        for (let mixinKey in mixinParam) {
          if (mixinParam.hasOwnProperty(mixinKey)) {
            bodyObj[mixinKey] = mixinParam[mixinKey]
          }
        }
        copyApi.body = ''
        for (let bodyKey in bodyObj) {
          if (bodyObj.hasOwnProperty(bodyKey)) {
            copyApi.body += `${bodyKey}=${bodyObj[bodyKey]}&`
          }
        }
        copyApi.body = copyApi.body.substr(0, copyApi.body.length - 1)
        //body为json字符串事
      } else {
        if (copyApi.body) {
          bodyObj = JSON.parse(copyApi.body)
          if (bodyObj.params) {
            for (let mixinKey in mixinParam) {
              if (
                mixinParam.hasOwnProperty(mixinKey) &&
                mixinParam[mixinKey] !== undefined
              ) {
                bodyObj.params[mixinKey] = mixinParam[mixinKey]
              }
            }
          } else {
            bodyObj.params = mixinParam
          }
        } else {
          bodyObj = {
            params: mixinParam,
          }
        }
        copyApi.body = JSON.stringify(bodyObj)
      }
    }
  }
  //如果传了time
  if (time) {
    let start = parseInt(time[0] / 1000)
    let end = parseInt(time[1] / 1000)
    let hasStart = false
    //如果是get
    let cha = end - start
    let interval = 0
    if (cha < 60 * 60) {
      interval = 60
    } else if (cha < 24 * 60 * 60) {
      interval = 24 * 60
    } else if (cha < 7 * 24 * 60 * 60) {
      interval = 7 * 24 * 60
    }
    if (copyApi.reqType === 'GET') {
      copyApi.urlParamList.forEach((item) => {
        if (item.key === 'start') {
          item.value = start
          hasStart = true
        }
        if (item.key === 'end') {
          item.value = end
        }
        if (item.key === 'interval') {
          item.value = interval
        }
      })
      if (!hasStart) {
        copyApi.urlParamList = copyApi.urlParamList.filter(
          (item) => item.key !== 'end'
        )
        copyApi.urlParamList.push(
          {
            key: 'start',
            value: start,
            type: 'time',
            timeFormat: 'timestamp',
            timeType: 'timestamp/1000',
          },
          {
            key: 'end',
            value: end,
            type: 'time',
            timeFormat: 'timestamp',
            timeType: 'timestamp/1000',
          }
        )
      }

      //如果是post
    } else {
      let type
      for (let head of copyApi.headers) {
        if (head.key === 'Content-Type') {
          type = head.value
        }
      }
      let body
      //type 为'application/x-www-form-urlencoded'
      if (type === 'application/x-www-form-urlencoded') {
        if (copyApi.body) {
          body = copyApi.body.split('&').map((item) => ({
            key: item.split('=')[0],
            value: item.split('=')[1],
          }))
          let hasStart = false
          let hasEnd = false
          body.forEach((item) => {
            if (item.key === 'start') {
              item.value = start
              hasStart = true
            }
            if (item.key === 'end') {
              item.value = end
              hasEnd = true
            }
            if (item.key === 'interval') {
              item.value = interval
            }
          })
          if (!hasStart || !hasEnd) {
            body = body.filter(
              (item) => item.key !== 'end' && item.key !== 'start'
            )
            body.push(
              { key: 'start', value: start },
              { key: 'end', value: end }
            )
          }
          body = body.map((item) => `${item.key}=${item.value}`).join('&')
          copyApi.body = body
        } else {
          copyApi.body = `start=${start}&end=${end}`
        }
        //body为json字符串事
      } else {
        if (copyApi.body) {
          body = JSON.parse(copyApi.body)
          if (body.params) {
            body.params.start = start
            body.params.end = end
            if (body.params.interval) {
              item.value = interval
            }
          } else {
            body.params = {
              start,
              end,
            }
          }
        } else {
          body = {
            params: {
              start,
              end,
            },
          }
        }
        body.params.start = body.params.start + ''
        body.params.end = body.params.end + ''
        copyApi.body = JSON.stringify(body)
      }
    }
  }
  return copyApi
}
